package com.douwong.student.fragment;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.douwong.student.R;
import com.douwong.student.base.BaseFragment;
import com.douwong.student.model.ExamResult;
import com.douwong.student.model.ListPlExamineItemBean;
import com.douwong.student.model.Qditem;
import com.douwong.student.popuwindow.ShowClassTestRecordPopuWindow;
import com.douwong.student.utils.HtmlUtils;
import com.douwong.student.utils.UIUtils;

import java.util.List;

import static com.douwong.student.R.id.ed_fill_blanks_answers;

/**
 * @autor yichao
 * @time 2017/5/1017:11
 * @des 测验记录填空题
 */

public class TestRecordFillFragment  extends BaseFragment{

    private View mContentView;
    private ImageView mIvHead;
    private AppCompatTextView mAppCompatTextView;
    private LinearLayout mLlTitleRoot;
    private ListPlExamineItemBean mListPlExamineItemBean;
    private final Qditem mQdItem;
    private List<ExamResult> mExamResultList;
    private EditText mEditText;
    private ShowClassTestRecordPopuWindow       mShowClassTestRecordPopuWindown;
    private Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;

    }

    @Override
    public void setExamResultList(List<ExamResult> examResultList) {
        super.setExamResultList(examResultList);
        mExamResultList = examResultList;
    }

    public TestRecordFillFragment(ListPlExamineItemBean listPlExamineItemBean) {
        super();
        mListPlExamineItemBean = listPlExamineItemBean;
        mQdItem = listPlExamineItemBean.getQdItem();
    }
    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        mContentView = UIUtils.inflate(getActivity(), R.layout.test_record_fill_fragment);
        mIvHead = (ImageView) mContentView.findViewById(R.id.iv_answer_show_pic);
        mAppCompatTextView = (AppCompatTextView) mContentView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContentView.findViewById(R.id.ll_choicefragme_title_root);
        mEditText = (EditText) mContentView.findViewById(ed_fill_blanks_answers);
        return mContentView;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mIvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             mShowClassTestRecordPopuWindown = new ShowClassTestRecordPopuWindow(mContext,mQdItem);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);

            }
        });
        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShowClassTestRecordPopuWindown = new ShowClassTestRecordPopuWindow(mContext,mQdItem);
                mShowClassTestRecordPopuWindown.showAtLocation(v, Gravity.CENTER,0,0);

            }
        });

    }
    @Override
    protected void initData() {
        super.initData();
        if (mQdItem!=null){
        String itemBody = mQdItem.getItemBody();
        //是否显示图片
        mIvHead.setVisibility(HtmlUtils.isShowImage(itemBody)?View.VISIBLE:View.GONE);
        //题目
        HtmlUtils.showItemBody(mAppCompatTextView,itemBody);
        }
        mEditText.setVisibility(View.VISIBLE);
        mEditText.setEnabled(false);
        if (mExamResultList == null) {
            return;
        }
        for (int i = 0; i < mExamResultList.size(); i++) {
            if (mListPlExamineItemBean.getItemId() == mExamResultList.get(i).getItemId()) {
                if (mExamResultList.get(i).getIsWaiver() == 1) {
                } else {
                    mEditText.setText(mExamResultList.get(i).getAnswers().replace("*#*", "  "));
                }
                break;
            }
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mShowClassTestRecordPopuWindown!=null && mShowClassTestRecordPopuWindown.isShowing()){
            mShowClassTestRecordPopuWindown.dismiss();

        }
    }
}
